package com.aohanyao.transformer.slice;

import com.aohanyao.transformer.ResourceTable;
import com.aohanyao.utils.PxUtil;
import com.aohanyao.viewpager.HorizontalViewPager;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.*;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.WindowManager;
import ohos.utils.PacMap;

import java.util.ArrayList;
import java.util.List;

/**
 * 卡片页面的展示
 */
public class HorizontalAbility extends Ability {
    int style = 3;
    List<Component> data = new ArrayList<>();
    HorizontalViewPager horizontalViewPager;
    ListContainer listContainer;

    @Override
    protected void onStart(Intent intent) {
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(new RgbColor(48, 62, 159).asArgbInt());
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_horzontal_layout);
        DependentLayout parent = (DependentLayout) findComponentById(ResourceTable.Id_parent);
        Image button = (Image) findComponentById(ResourceTable.Id_switch_style);
        horizontalViewPager = (HorizontalViewPager) findComponentById(ResourceTable.Id_hor_layout);
        horizontalViewPager.setMarginTop(0);
        List<String> list = new ArrayList<>();
        list.add("Top");
        list.add("Top Left");
        list.add("Top Right");
        list.add("Bottom");
        list.add("Bottom Left");
        list.add("Bottom Right");
        list.add("Right");
        list.add("Left");

        listContainer = (ListContainer) findComponentById(ResourceTable.Id_show_style);
        listContainer.setVisibility(Component.HIDE);
        parent.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (listContainer.getVisibility() == Component.VISIBLE) {
                    listContainer.setVisibility(Component.HIDE);
                    horizontalViewPager.setEnable(true);
                }
            }
        });
        listContainer.setItemProvider(new BaseItemProvider() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int i) {
                return list.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
                final Component cpt;
                if (component == null) {
                    cpt = new Text(getContext());
                } else {
                    cpt = component;
                }

                ((Text) cpt).setText(list.get(i));
                cpt.setWidth(listContainer.getWidth());//保证都能点击到
                cpt.setHeight(150);
                ((Text) cpt).setTextSize(50);
                ((Text) cpt).setTextAlignment(TextAlignment.VERTICAL_CENTER);
                cpt.setPaddingLeft(30);
//                cpt.setMarginsTopAndBottom(40,40);
                Element element = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_btn_ripple_bg);
                setButtonsRippleColor(element, cpt);

                return cpt;
            }
        });

        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                if (style == i) {
                    listContainer.setVisibility(Component.HIDE);
                    horizontalViewPager.setEnable(true);
                    return;
                }
                style = i;
                listContainer.setVisibility(Component.HIDE);
                horizontalViewPager.setEnable(true);
                horizontalViewPager.setType(i);
                if (i < 3) {
                    horizontalViewPager.setMarginTop((int) PxUtil.vp2px(60));
                } else {
                    horizontalViewPager.setMarginTop(0);
                }
            }
        });

        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                listContainer.setVisibility(Component.VISIBLE);
                listContainer.getComponentParent().moveChildToFront(listContainer);
                horizontalViewPager.setEnable(false);
            }
        });
        int size = 15;
        for (int i = 0; i < size; i++) {
            Text text1 = new Text(getContext());
            text1.setTextSize(80, Text.TextSizeType.FP);
            text1.setTextAlignment(TextAlignment.CENTER);
            text1.setText(i + 1 + "");
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setStroke(1, new RgbColor(176, 176, 176));
            shapeElement.setCornerRadius(15);
            shapeElement.setRgbColor(new RgbColor(254, 254, 254));
            text1.setBackground(shapeElement);
            data.add(text1);
        }
        horizontalViewPager.setType(3);
        horizontalViewPager.setData(data);
    }

    /**
     * 设置item的点击效果
     *
     * @param element   背景内容
     * @param component 指定的控件
     */
    public void setButtonsRippleColor(Element element, Component component) {
        Element resultElement = null;
        if (element instanceof PixelMapElement) {
            resultElement = (PixelMapElement) element;
        } else if (element instanceof VectorElement) {
            resultElement = (VectorElement) element;
        } else {
            resultElement = element;
        }
        component.setBackground(resultElement);
    }

    /**
     * 返回键事件处理
     */
    @Override
    protected void onBackPressed() {
        if (listContainer.getVisibility() == Component.VISIBLE) {
            listContainer.setVisibility(Component.HIDE);
        } else {
            super.onBackPressed();
        }
    }

    List<Component> add = new ArrayList<Component>();
    List<Component> remove = new ArrayList<Component>();

    /**
     * 转屏保存状态
     *
     * @param outState 转屏状态的保存
     */
    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);
        add = horizontalViewPager.getRestoreAdd();
        remove = horizontalViewPager.getResotreRemove();
        outState.putIntValue("add", add.size());
        outState.putIntValue("remove", remove.size());
    }


    /**
     * 转屏恢复状态
     *
     * @param inState 转屏状态的恢复
     */
    @Override
    public void onRestoreAbilityState(PacMap inState) {
        super.onRestoreAbilityState(inState);
        int removeT = inState.getIntValue("remove");
        for (int i = 0; i < removeT; i++) {
            remove.add(data.get(i));
        }
        for (int j = removeT; j < data.size(); j++) {
            add.add(data.get(j));
        }
        horizontalViewPager.setRestoreStata(add, remove);
    }
}
